package com.nwpu.bigdata.service.impl;

import com.nwpu.bigdata.dao.mapper.FaultKeyMapper;
import com.nwpu.bigdata.dto.BackendResult;
import com.nwpu.bigdata.model.FaultKey;
import com.nwpu.bigdata.model.FaultKeyExample;
import com.nwpu.bigdata.model.FaultKeyExtend;
import com.nwpu.bigdata.model.User;
import com.nwpu.bigdata.service.FaultLogKeyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author carlin
 * @date 2018/8/1 0001 16:29
 */
@Service
public class FaultLogKeyServiceImpl implements FaultLogKeyService {

    private static final Logger logger = LoggerFactory.getLogger(FaultDetectionServiceImpl.class);

    @Autowired
    FaultKeyMapper faultKeyMapper;

    @Override
    public BackendResult<FaultKey> setFaultLogKey(FaultKey faultKey) {
        int num = faultKeyMapper.insert(faultKey);
        if (num > 0) {
            logger.info("fault log key insert successfully into db!");
            return new BackendResult<FaultKey>(true, faultKey);
        } else {
            logger.error("fault log key insert fail into db!");
            return new BackendResult<FaultKey>(false, "fault log key insert fail into db!");
        }
    }

    @Override
    public BackendResult<List<FaultKeyExtend>> getFaultKeyExtend() {
        List<FaultKeyExtend> faultKeyExtends = faultKeyMapper.selectFaultKeyExtend();
        if (faultKeyExtends != null && faultKeyExtends.size() != 0) {
            return new BackendResult<>(true, faultKeyExtends);
        } else {
            return new BackendResult<>(false, "fault keywords is empty, no set.");
        }
    }

    @Override
    public boolean updateFaultLogKey(FaultKey faultKey) {
        int num = faultKeyMapper.insert(faultKey);
        if (num < 0) {
            logger.info("fault log key insert successfully into db!");
            return true;
        } else {
            logger.error("fault log key insert fail into db!");
            return false;
        }
    }

    @Override
    public BackendResult<List<FaultKey>> listFaultKey(User user) {
        FaultKeyExample faultKeyExample = new FaultKeyExample();
        FaultKeyExample.Criteria criteria = faultKeyExample.createCriteria();
        criteria.andUserIdEqualTo(user.getId());
        List<FaultKey> faultKeys = faultKeyMapper.selectByExample(faultKeyExample);
        if (faultKeys != null) {
            logger.info("return faultkeys size is {} !", faultKeys.size());
            return new BackendResult<List<FaultKey>>(true, faultKeys);
        } else {
            logger.error("user no config keys!");
            return new BackendResult<List<FaultKey>>(false, "user no config keys!");
        }

    }


}
